﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardLogistics.Model.Basic;
using System.Data;
using System.Data.OracleClient;

namespace CardLogistics.DAL.Basic
{
    /// <summary>
    /// 基础数据卡类型
    /// </summary>
    public class Basic_CardTypeData
    {
        #region 新增
        /// <summary>
        /// 新增销售机构/网点信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string Add(Basic_CardType model)
        {
            try
            {
                OracleParameter[] parameters = {
					new OracleParameter("vID", OracleType.VarChar,100),
					new OracleParameter("vCARDTYPENAME", OracleType.VarChar),
					new OracleParameter("vMAINCODE", OracleType.VarChar),
					new OracleParameter("vCHILDCODE", OracleType.VarChar),
					new OracleParameter("vREMARK", OracleType.VarChar),
					new OracleParameter("vSTATUS", OracleType.Number),
					new OracleParameter("vISDELETE", OracleType.Number),
                    new OracleParameter("vLASTUPDATEUSER", OracleType.VarChar),
                    new OracleParameter("vSTARTCODE", OracleType.Number),
                    new OracleParameter("vFatherCardTypeId", OracleType.VarChar)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.CARDTYPENAME;
                parameters[2].Value = model.MAINCODE;
                parameters[3].Value = model.CHILDCODE;
                parameters[4].Value = model.REMARK;
                parameters[5].Value = model.STATUS;
                parameters[6].Value = model.ISDELETE;
                parameters[7].Value = model.LASTUPDATEUSER;
                parameters[8].Value = model.StartCode;
                parameters[9].Value = model.FatherCardTypeId;
                string StrOutPar = DbHelperSQL.RunProcedure("P_Basic_CardType_ADD", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>修改成功返回true、否则返回false</retu     rns>
        public string Update(Basic_CardType model)
        {
            try
            {
                OracleParameter[] parameters = {
					new OracleParameter("vID", OracleType.VarChar),
					new OracleParameter("vCARDTYPENAME", OracleType.VarChar),
					new OracleParameter("vMAINCODE", OracleType.VarChar),
					new OracleParameter("vCHILDCODE", OracleType.VarChar),
					new OracleParameter("vREMARK", OracleType.VarChar),
					new OracleParameter("vSTATUS", OracleType.Number),
					new OracleParameter("vISDELETE", OracleType.Number),
                    new OracleParameter("vLASTUPDATEUSER", OracleType.VarChar),
                    new OracleParameter("vSTARTCODE", OracleType.Number),
                    new OracleParameter("vFatherCardTypeId", OracleType.VarChar),
                    new OracleParameter("varOut", OracleType.VarChar,100)};
                parameters[0].Value = model.ID;
                parameters[1].Value = model.CARDTYPENAME;
                parameters[2].Value = model.MAINCODE;
                parameters[3].Value = model.CHILDCODE;
                parameters[4].Value = model.REMARK;
                parameters[5].Value = model.STATUS;
                parameters[6].Value = model.ISDELETE;
                parameters[7].Value = model.LASTUPDATEUSER;
                parameters[8].Value = model.StartCode;
                parameters[9].Value = model.FatherCardTypeId;
                parameters[10].Direction = ParameterDirection.Output;
                return DbHelperSQL.RunProcedure("P_Basic_CardType_Update", parameters, parameters[10]).ToString();
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("VarOut",  OracleType.VarChar,100),
					    new OracleParameter("vID",  OracleType.VarChar)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_Basic_CardType_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "v_Basic_CardType", StrFields, "ID", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ID = '" + id + "' ", "*");
        }
        #endregion

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Basic_CardType GetModel(string Id)
        {
            Basic_CardType model = new Basic_CardType();
            DataRow row = GetInfoById(Id).Rows[0];
            if (row != null)
            {
                if (row["ID"] != null)
                {
                    model.ID = row["ID"].ToString();
                }
                if (row["CARDTYPENAME"] != null)
                {
                    model.CARDTYPENAME = row["CARDTYPENAME"].ToString();
                }
                if (row["MAINCODE"] != null)
                {
                    model.MAINCODE = row["MAINCODE"].ToString();
                }
                if (row["CHILDCODE"] != null)
                {
                    model.CHILDCODE = row["CHILDCODE"].ToString();
                }
                if (row["REMARK"] != null)
                {
                    model.REMARK = row["REMARK"].ToString();
                }
                if (row["STATUS"] != null && row["STATUS"].ToString() != "")
                {
                    model.STATUS = decimal.Parse(row["STATUS"].ToString());
                }
                if (row["ISDELETE"] != null && row["ISDELETE"].ToString() != "")
                {
                    model.ISDELETE = decimal.Parse(row["ISDELETE"].ToString());
                }
                if (row["LASTUPDATEUSER"] != null)
                {
                    model.LASTUPDATEUSER = row["LASTUPDATEUSER"].ToString();
                }
                if (row["LASTUPDATEDATE"] != null && row["LASTUPDATEDATE"].ToString() != "")
                {
                    model.LASTUPDATEDATE = DateTime.Parse(row["LASTUPDATEDATE"].ToString());
                }
                if (row["StartCode"] != null && row["StartCode"].ToString() != "")
                {
                    model.StartCode = decimal.Parse(row["StartCode"].ToString());
                }
                if (row["FatherCardTypeId"] != null && row["FatherCardTypeId"].ToString() != "")
                {
                    model.FatherCardTypeId =row["FatherCardTypeId"].ToString(); //所属卡类型
                }
            }
            return model;
        }
    }
}